package com.shop.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.shop.mapper.ManagerListMapper;
import com.shop.mapper.PermissionMapper;
import com.shop.mapper.RoleMapper;
import com.shop.pojo.Manager;
import com.shop.pojo.Permission;
import com.shop.pojo.Role;
import com.shop.service.ManagerListService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.Set;

@Service(interfaceClass = ManagerListService.class)
@Transactional
public class ManagerListServiceImpl implements ManagerListService {
    @Autowired
    private ManagerListMapper managerListMapper;
    @Autowired
    private RoleMapper roleMapper;
    @Autowired
    private PermissionMapper permissionMapper;

    //根据用户名查询数据库用户信息和关联的角色信息，同时查询角色关联的权限信息
    @Override
    public Manager findByUsername(String username) {
        QueryWrapper wrapper=new QueryWrapper();
        wrapper.eq("username",username);
        Manager manager = managerListMapper.selectOne(wrapper);
        if (manager==null){
            return null;
        }
        Integer managerId = manager.getId();
        Set<Role> roles = roleMapper.findByUserId(managerId);
        for (Role role : roles) {
            Integer roleId = role.getId();
            //根据角色ID查询关联的权限
            Set<Permission> permissions = permissionMapper.findByRoleId(roleId);
            role.setPermissions(permissions);
        }
        manager.setRoles(roles);
        return manager;
    }
}
